var layer = layui.layer;

var $firstA = $("#index_menu .layui-nav-item:first>a");
if($firstA.attr("data-url")){
    window.homeHash = $firstA.attr("data-url");
}else{
    window.homeHash = $("#index_home").data("url");
}

var title = document.title;
var hash = (location.hash == "" || location.hash.length === 1) ? homeHash : location.hash.substring(1);
var url = location.protocol + '//' + location.host + location.pathname + location.search + '#' + hash;
history.replaceState(null, document.title, url);
hashchange();


//hash监听，用于回退及点击菜单
window.addEventListener('hashchange', hashchange, false);

//监听菜单的点击事件
$(document).on("click","a[data-url]", function (e) {
    var hash = e.target.getAttribute("data-url");
    jump(hash);
});

function jump(hash) {
    if(location.hash == "#"+hash){
        hashchange();
    }else {
        location.hash = hash;
    }
}

/**
 * hashchange的回调函数，渲染右侧
 * @author zhaoqf
 */
function hashchange() {
    var hash = (location.hash == "" || location.hash.length === 1) ? homeHash : location.hash.substring(1);
	hash = decodeURIComponent(hash);
	hash = safeStr(hash);
    hash = hash.replace(/\[.*\]/,"");
    console.log(path);
    var path = window.basePath+"/admin/views/" + hash +".html";
    renderHtml({
        targetId: 'index_right',
        template: path
    });
    var $a = $(".fsh-menu a[data-url='" + hash + "']");
    if($a.length > 0) {
        $a.parent().addClass('layui-this');
    }
    layer.closeAll();
}

function safeStr(str){
	return str.replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/"/g, "&quot;").replace(/'/g, "&#039;");
}